package com.mingqijia.gassafety.shared.utils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName EntityUtils.java

 * @createTime 2022年05月12日
 */
public class EntityUtils {

 public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
  T t = null;
  try {
   t = entity.newInstance();
   for (Field field : entity.getDeclaredFields()) {
    if (map.containsKey(field.getName())) {
     boolean flag = field.isAccessible();
     field.setAccessible(true);
     Object object = map.get(field.getName());
     if (object != null && field.getType().isAssignableFrom(object.getClass())) {
      field.set(t, object);
     }
     field.setAccessible(flag);
    }
   }
   return t;
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  }
  return t;
 }
}